<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/10
 * Time: 15:57
 */

namespace app\admin\model;


class GoodsCategory extends BaseModel
{
    //此处为admin/food控制器调用的方法获取组合后的分类数据(此处为递归查询,查询结果为树形数组)
    public static function getGoodsCategory($parent_id=0)
    {
        $res = self::where(['parent_id'=>$parent_id])->order(['id'=>'asc'])->select();
        foreach($res as $k=>$v){
            $res[$k]->data('child',self::getGoodsCategory($v->id));
        }
        return $res;
    }

    //返回完整排序后的数据
    public static function getGoodsOrder()
    {
        return self::getTree(self::field('id,cates_name,parent_id,level')->order(['level'=>'asc'])->select());
    }

    //获取二维数组排序的方法
    private static function getTree($data,$parent_id=0)
    {
        static $_ret = array();
        foreach($data as $k=>$v){
            if($v->parent_id == $parent_id){
                $_ret[] = $v;
                self::getTree($data,$v->id);
//                $_ret = array_merge($_ret,self::getTree($data,$v->id));
            }
        }
        return $_ret;
    }

    public static function getCategory()
    {
        $result = self::where('is_recommend','=','1')->limit(2)->select();
        return $result;
    }
}